﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using douban.Core.ViewModels;
using Windows.UI;
using douban.app.Controls;
using douban.Core.Apis;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上提供

namespace douban.app.Views
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : PageBase
    {
        public MainPage()
        {
            this.InitializeComponent();

            var accentColor = App.Current.Resources["SystemAccentColor"] as SolidColorBrush;

            Core.Utils.UiUtil.ShowSystemTrayAsync(accentColor.Color, Colors.Black, text: MainVm. AssemblyTitle);
        }


        private void TopicControl_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var control = sender as TopicControl;
            this.Frame.Navigate(typeof (TopicPage), control?.DataContext);
        }

        private void BarButtonToTop_OnClick(object sender, RoutedEventArgs e)
        {
            var pivotItem = PivotMain.SelectedItem as PivotItem;
            var listView = pivotItem?.Content as ListView;

            if (listView?.Items != null && listView.Items.Count > 0)
            {
                var item0 = listView.Items[0];
                listView.ScrollIntoView(item0, ScrollIntoViewAlignment.Leading);
            }
        }

        private async void BarButtonRefresh_OnClick(object sender, RoutedEventArgs e)
        {
            var pivotItem = PivotMain.SelectedItem as PivotItem;
            var listView = pivotItem?.Content as ListView;
            var datasource = listView?.ItemsSource as IncrementalLoadingCollection<Topic>;
            if (datasource != null)
                await datasource.Refresh();
        }
    }
}
